package cn.wit.test;

import java.util.Arrays;
import java.util.Comparator;

public class DemoComparator {
	public static void main(String[] args) {
		String[] strings = { "cd", "abc", "efg", "abcd", "gef" };

		// 对数组进行排序,字符串排序，默认选择字典序
		// Arrays.sort(strings);
		//
		// System.out.println(Arrays.toString(strings));

		// 按照长度来排序
		// Arrays.sort(strings, comparator);
		LengthComparator comparator =new LengthComparator();

		Arrays.sort(strings, comparator);
		
		System.out.println(Arrays.toString(strings));
	}
}
//根据字符串长度去排序
	class LengthComparator implements Comparator<String> {

		// 如果left>right，返回整数
		// 返回0表示相等
		@Override
		public int compare(String left, String right) {
			// TODO Auto-generated method stub
			if (left.length() > right.length()) {
				return 1;
			} else if (left.length() == right.length()) {
				return 0;
			} else {
				return -1;
			}
		}

	}
